package innerInterceptor;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
/*
 * extends ActionSupport 错误处事流程
 */
public class UploadAction extends ActionSupport{
	private File file;//代表临时文件
	private String fileFileName;//原始名称
	private String fileContentType;//原始类型
	
	public String execute() {
		String path=ServletActionContext.getServletContext().getRealPath("/images");
		//将临时文件copy到目录目录
		try {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			File toSave = new File(path+"/"+fileFileName);
			BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(toSave));
			int b;
			while((b=in.read())!=-1){
				out.write(b);
			}
			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println(fileFileName+fileContentType);
		return "success";
	}
	
	public File getFile() {
		return file;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	
}
